﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using GestionTaxesRiaApp.Web;
using System.ServiceModel.DomainServices.Client;

namespace GestionTaxesRiaApp.Views
{
    public partial class GestionFrequentation : Page
    {

        public static Frequentation FrequentationStatic;

        GestionTaxesContext gestionTaxesContext = new GestionTaxesContext();

        public GestionFrequentation()
        {
            InitializeComponent();
            SetDroits();
            frequentationDomainDataSource.SubmittedChanges += new System.EventHandler<SubmittedChangesEventArgs>(frequentationDomainDataSource_SubmittedChanges);
            
        }
        void FrequentationLoadedCallback(LoadOperation<Frequentation> loadOperation)
        {
            if (Connexion.utilisateurStatic.uti_IdFonction == Home.hebergeurInt)
            {
                frequentationDataGrid.ItemsSource = loadOperation.Entities;
            }
        }
        // S'exécute lorsque l'utilisateur navigue vers cette page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void frequentationDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void AjouterLogementBtn_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Browser.HtmlPage.Window.Navigate(new Uri("/GestionTaxesRiaAppTestPage.aspx#/AjouterFrequentation", UriKind.Relative));
        }

        private void SauvegarderChangementBtn_Click(object sender, RoutedEventArgs e)
        {
            Frequentation myFrequentation = frequentationDataGrid.SelectedItem as Frequentation;
            if (myFrequentation.fre_IsValider == 0)
            {
                frequentationDomainDataSource.DomainContext.SubmitChanges();
            }
            else
            {
                MessageBox.Show("Modification impossible, la ligne a déjà été validée");
            }
        }

        private void SupprimerBtn_Click(object sender, RoutedEventArgs e)
        {
            Frequentation myFrequentation = frequentationDataGrid.SelectedItem as Frequentation;
            if (myFrequentation.fre_IsValider == 0)
            {
                (frequentationDomainDataSource.DomainContext as GestionTaxesContext).Frequentations.Remove(myFrequentation);
                frequentationDomainDataSource.DomainContext.SubmitChanges();
            }
            else
            {
                MessageBox.Show("Suppression impossible, la ligne a déjà été validée");
            }

        }

        private void SetDroits()
        {
            if (Connexion.utilisateurStatic.uti_IdFonction == Home.decoInt)
            {
                System.Windows.Browser.HtmlPage.Window.Navigate(new Uri("/GestionTaxesRiaAppTestPage.aspx#/Connexion", UriKind.Relative));
            }
            if (Connexion.utilisateurStatic.uti_IdFonction == Home.hebergeurInt)
            {
                ValiderBtn.Visibility = System.Windows.Visibility.Collapsed;
                DetailValidationGrid.Visibility = System.Windows.Visibility.Collapsed;
                var frequentation = gestionTaxesContext.Load<Frequentation>(gestionTaxesContext.GetFrequentationsByProprietaireIdQuery(Connexion.utilisateurStatic.uti_Id), FrequentationLoadedCallback, false);
            }
            if (Connexion.utilisateurStatic.uti_IdFonction == Home.lecteurInt
                || Connexion.utilisateurStatic.uti_IdFonction == Home.gestionnaireInt)
            {
                AjouterLogementBtn.Visibility = System.Windows.Visibility.Collapsed;
                SupprimerBtn.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        private void ValiderBtn_Click(object sender, RoutedEventArgs e)
        {
            if(Int32.Parse(fre_IsValiderTextBox.Text) == 0)
            {
                fre_IsValiderTextBox.Text = "1";
                frequentationDomainDataSource.SubmitChanges();
            }
            else
            {
                MessageBox.Show("Cette fréquentation a déjà été validée");
            }
        }
        void frequentationDomainDataSource_SubmittedChanges(object sender, SubmittedChangesEventArgs e)
        {
            if (e.HasError)
            {
                System.Windows.MessageBox.Show("Erreur pendant la mise à jour !\n" + e.Error.Message);
                e.MarkErrorAsHandled();
            }
            else
            {
                if (e.ChangeSet.ModifiedEntities.Count != 0)
                    System.Windows.MessageBox.Show(e.ChangeSet.ModifiedEntities.Count
                        + " entitée(s) modifiée(s).");
                if (e.ChangeSet.AddedEntities.Count != 0)
                    System.Windows.MessageBox.Show(e.ChangeSet.AddedEntities.Count
                        + " entitée(s) ajoutée(s).");
                if (e.ChangeSet.RemovedEntities.Count != 0)
                    System.Windows.MessageBox.Show(e.ChangeSet.RemovedEntities.Count
                        + " entitée(s) supprimée(s).");
            }
        }

        private void frequentationDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            
            if (fre_IdLogementTextBox.Text!= "")
            {
                if (Connexion.utilisateurStatic.uti_IdFonction == Home.gestionnaireInt
                    || Connexion.utilisateurStatic.uti_IdFonction == Home.administrateurInt)
                {
                    ValiderBtn.Visibility = System.Windows.Visibility.Visible;
                    DetailValidationGrid.Visibility = System.Windows.Visibility.Visible;
                    LogementTB.Visibility = System.Windows.Visibility.Visible;
                    ProprietaireTB.Visibility = System.Windows.Visibility.Visible;
                    var frequentation = gestionTaxesContext.Load<Frequentation>(gestionTaxesContext.GetFrequentationsByProprietaireIdQuery(Connexion.utilisateurStatic.uti_Id), FrequentationLoadedCallback, false);
                }
                var proprietaire = gestionTaxesContext.Load<Utilisateur>(gestionTaxesContext.GetUtilisateurByIdQuery(int.Parse(fre_IdProprietaireTextBox.Text)), ProprietaireLoadedCallback, false);
                var logement = gestionTaxesContext.Load<Logement>(gestionTaxesContext.GetLogementByIdQuery(int.Parse(fre_IdLogementTextBox.Text)), LogementLoadedCallback, false);
            }
            if (fre_IsValiderTextBox.Text == 1.ToString())
            {
                ValiderBtn.Visibility = System.Windows.Visibility.Collapsed;
            }
        }
        void ProprietaireLoadedCallback(LoadOperation<Utilisateur> loadOperation)
        {
            Utilisateur proprietaire = loadOperation.Entities.FirstOrDefault();
            ProprietaireTB.Text = proprietaire.uti_Prenom + " " + proprietaire.uti_Nom;

        }
        void LogementLoadedCallback(LoadOperation<Logement> loadOperation)
        {
            Logement logement = loadOperation.Entities.FirstOrDefault();
            LogementTB.Text = logement.log_Nom;

        }

      
        
    }
}
